﻿using UnityEngine;
using System.Collections.Generic;

namespace winS.Unity
{
    /// <summary>
    /// 二阶贝塞尔曲线
    /// </summary>
    public class BezierCurveTwo
    {
        /// <summary>
        /// 所有线段
        /// </summary>
        public List<Segment> segments { get; private set; }

        public Segment this[int index] => segments[index];

        public BezierCurveTwo()
        {
            segments = new List<Segment>();
        }

        /// <summary>
        /// 添加一段二阶贝赛尔曲线
        /// </summary>
        public Segment AddSegment()
        {
            Segment segment = new Segment();
            segments.Add(segment);
            return segment;
        }
        /// <summary>
        /// 移除一段二阶贝赛尔曲线
        /// </summary>
        public void RemoveSegment(Segment segment)
        {
            segments.Remove(segment);
        }
        /// <summary>
        /// 根据索引 <paramref name="index"/> 移除一段二阶贝赛尔曲线
        /// </summary>
        public void RemoveSegment(int index)
        {
            segments.RemoveAt(index);
        }

        /// <summary>
        /// 一段二阶贝塞尔曲线
        /// </summary>
        public class Segment
        {
            /// <summary>
            /// 起始点
            /// </summary>
            public Vector3 startPoint;
            /// <summary>
            /// 结束点
            /// </summary>
            public Vector3 endPoint;
            /// <summary>
            /// 控制点
            /// </summary>
            public Vector3 controlPoint;

            /// <summary>
            /// 获取位置
            /// </summary>
            /// <param name="t"></param>
            public Vector3 GetPoint(float t)
            {
                return Vector3.Lerp(Vector3.Lerp(startPoint, controlPoint, t), Vector3.Lerp(controlPoint, endPoint, t), t);
            }
        }
    }
}

